#include "common.utils.hpp"

// Poll or Wait

void test_event()
{
    INIT_ALL_FUNC;
    MOSTCOMMWINDOWANDRENDERER

    SDL_SetRenderDrawColor(renderer, COLORSET(60,c0,a0,ff));

    SDL_RenderClear(renderer);

    Present(renderer);

    SDL_Event event;
#if 0
    while(true)
    {
        // INFO: Poll
        while(SDL_PollEvent(&event))
        {
            switch (event.type) {
                case SDL_QUIT:
                    puts("Quit app");
                    return;
                case SDL_MOUSEBUTTONDOWN:
                    printf("Button down at: (%d, %d)\n",event.button.x, event.button.y);
                    break;
                case SDL_MOUSEBUTTONUP:
                    printf("Button up at: (%d, %d)\n",event.button.x, event.button.y);
                    break;
                case SDL_KEYDOWN:
                    printf("key down: %s\n", SDL_GetKeyName(event.key.keysym.sym));
                    fflush(stdout);// instance clear buffer instead quit app then print
                    if (event.key.keysym.sym == SDLK_ESCAPE)// type esc to quit
                    {
                        SDL_Log("Quit app");
                        return;
                    }

                case SDL_KEYUP:
                    printf("key up: %s\n", SDL_GetKeyName(event.key.keysym.sym));

                    break;
                default:
                    break;
            }
            fflush(stdout);// for buffer present
        }
        DELAY(5);
    }
#endif
        // INFO: Wait can escape the fucking while(true), 适合没有动画的页面
        while(SDL_WaitEvent(&event))
        {
            switch (event.type) {
                case SDL_QUIT:
                    SDL_Log("Quit app");
                    return;
                case SDL_MOUSEBUTTONDOWN:
                    printf("Button down at: (%d, %d)\n",event.button.x, event.button.y);
                    break;
                case SDL_MOUSEBUTTONUP:
                    printf("Button up at: (%d, %d)\n",event.button.x, event.button.y);
                    break;
                case SDL_KEYDOWN:
                    printf("key down: %s\n", SDL_GetKeyName(event.key.keysym.sym));
                    fflush(stdout);// instance clear buffer instead quit app then print
                    if (event.key.keysym.sym == SDLK_ESCAPE)// type esc to quit
                    {
                        SDL_Log("Quit app");
                        return;
                    }

                case SDL_KEYUP:
                    printf("key up: %s\n", SDL_GetKeyName(event.key.keysym.sym));

                    break;
                default:
                    break;
            }
            fflush(stdout);// for buffer present
        }
        DELAY(5);

    MOSTCOMMWINDOWANDRENDER_FREE

    SDL_Quit();
}